package cn.com.chengmandian.bike.controller;

import cn.com.chengmandian.bike.dto.PhoneAuthSendForm;
import cn.com.chengmandian.bike.dto.PhoneLoginForm;
import cn.com.chengmandian.bike.log.ActionLog;
import cn.com.chengmandian.bike.model.EbMemberInfo;
import cn.com.chengmandian.bike.service.IEbMemberBlackService;
import cn.com.chengmandian.bike.service.IEbMemberInfoService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.service.bike.BikeCommonClient;
import cn.com.chengmandian.service.bike.constants.AuthTypeEnum;
import cn.com.chengmandian.service.bike.form.AuthCheckForm;
import cn.com.chengmandian.service.bike.form.AuthSendForm;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "手机号相关接口")
@Slf4j
@RestController
@RequestMapping("/phone")
public class PhoneController {
    @Autowired
    private BikeCommonClient bikeCommonClient;

    @Autowired
    private IEbMemberInfoService memberInfoService;

    @Autowired
    private IEbMemberBlackService memberBlackService;

    @ApiOperation("发送验证码(无需登录)")
    @PostMapping("/auth/send")
    public AjaxResult authSend(@RequestBody PhoneAuthSendForm phoneAuthSendForm) {
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        AuthSendForm authSendForm = new AuthSendForm().setPhone(phoneAuthSendForm.getPhone()).setType(AuthTypeEnum.MEMBER_LOGIN.getCode());
        return bikeCommonClient.authSend(agentInfo.getId(), authSendForm);
    }

    @ApiOperation("手机号登陆/注册(无需登录)")
    @PostMapping("/login")
    @ActionLog(name="手机号登陆/注册",description = "'参数：' + #phoneLoginForm")
    public AjaxResult login(@RequestBody PhoneLoginForm phoneLoginForm) {
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        AuthCheckForm authCheckForm = new AuthCheckForm().setPhone(phoneLoginForm.getPhone()).setCode(phoneLoginForm.getCode()).setType(AuthTypeEnum.MEMBER_LOGIN.getCode());
        AjaxResult res = bikeCommonClient.authCheck(agentInfo.getId(), authCheckForm);
        if (!res.isSuccess()) return res;
        EbMemberInfo memberInfo = memberInfoService.getByPhone(phoneLoginForm.getPhone());
        //判断手机号是否拉黑
        memberBlackService.checkPhone(phoneLoginForm.getPhone());
        if(ObjectUtil.isNotNull(memberInfo)){
            // 检验身份证号
            memberBlackService.checkIdCard(memberInfo.getIdCard());
        }
        if (memberInfo != null) {
            Assert.isTrue(memberInfo.getIsEnabled().equals(CommonConstant.ONE), "您的账号已被禁用");
        } else {
            memberInfo = memberInfoService.createMember(agentInfo.getAreaId(), phoneLoginForm.getPhone(), null, null, null);
        }
        String token = memberInfoService.getToken(memberInfo);
        return AjaxResult.success(MapUtil.of("token", token));
    }
}
